{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "e5b61574",
   "metadata": {},
   "source": [
    "## 1、填充生命体征数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "49bb264a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>受试者编号</th>\n",
       "      <th>性别编码</th>\n",
       "      <th>年龄段编码</th>\n",
       "      <th>BMI编码</th>\n",
       "      <th>手术史_分类</th>\n",
       "      <th>既往史_分类</th>\n",
       "      <th>镇静药名称</th>\n",
       "      <th>sbp00</th>\n",
       "      <th>dbp00</th>\n",
       "      <th>petco200</th>\n",
       "      <th>...</th>\n",
       "      <th>IPI10</th>\n",
       "      <th>moaas10</th>\n",
       "      <th>sbpjieshu</th>\n",
       "      <th>dbpjieshu</th>\n",
       "      <th>petco2jieshu</th>\n",
       "      <th>RRjieshu</th>\n",
       "      <th>spo2jieshu</th>\n",
       "      <th>HRjieshu</th>\n",
       "      <th>IPIjieshu</th>\n",
       "      <th>moaasjieshu</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>152</td>\n",
       "      <td>99</td>\n",
       "      <td>38.0</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>117.0</td>\n",
       "      <td>78.0</td>\n",
       "      <td>8.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>100.0</td>\n",
       "      <td>88.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>141</td>\n",
       "      <td>90</td>\n",
       "      <td>37.0</td>\n",
       "      <td>...</td>\n",
       "      <td>4.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>129.0</td>\n",
       "      <td>84.0</td>\n",
       "      <td>19.0</td>\n",
       "      <td>15.0</td>\n",
       "      <td>100.0</td>\n",
       "      <td>82.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>158</td>\n",
       "      <td>109</td>\n",
       "      <td>35.0</td>\n",
       "      <td>...</td>\n",
       "      <td>8.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>114.0</td>\n",
       "      <td>76.0</td>\n",
       "      <td>20.0</td>\n",
       "      <td>23.0</td>\n",
       "      <td>99.0</td>\n",
       "      <td>72.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>182</td>\n",
       "      <td>66</td>\n",
       "      <td>29.0</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>105.0</td>\n",
       "      <td>55.0</td>\n",
       "      <td>11.0</td>\n",
       "      <td>13.0</td>\n",
       "      <td>98.0</td>\n",
       "      <td>68.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>136</td>\n",
       "      <td>87</td>\n",
       "      <td>40.0</td>\n",
       "      <td>...</td>\n",
       "      <td>3.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>98.0</td>\n",
       "      <td>65.0</td>\n",
       "      <td>45.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>100.0</td>\n",
       "      <td>83.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 95 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   受试者编号  性别编码  年龄段编码  BMI编码  手术史_分类  既往史_分类  镇静药名称  sbp00  dbp00  petco200  \\\n",
       "0      1     1      3      1       0       0      0    152     99      38.0   \n",
       "1      2     1      3      2       0       0      0    141     90      37.0   \n",
       "2      3     0      2      3       0       0      1    158    109      35.0   \n",
       "3      4     1      3      2       0       2      1    182     66      29.0   \n",
       "4      5     1      2      1       0       0      1    136     87      40.0   \n",
       "\n",
       "   ...  IPI10  moaas10  sbpjieshu  dbpjieshu  petco2jieshu  RRjieshu  \\\n",
       "0  ...    NaN      NaN      117.0       78.0           8.0       0.0   \n",
       "1  ...    4.0      0.0      129.0       84.0          19.0      15.0   \n",
       "2  ...    8.0      1.0      114.0       76.0          20.0      23.0   \n",
       "3  ...    NaN      NaN      105.0       55.0          11.0      13.0   \n",
       "4  ...    3.0      0.0       98.0       65.0          45.0       5.0   \n",
       "\n",
       "   spo2jieshu  HRjieshu  IPIjieshu  moaasjieshu  \n",
       "0       100.0      88.0        1.0          0.0  \n",
       "1       100.0      82.0        4.0          0.0  \n",
       "2        99.0      72.0        5.0          0.0  \n",
       "3        98.0      68.0        4.0          0.0  \n",
       "4       100.0      83.0        6.0          0.0  \n",
       "\n",
       "[5 rows x 95 columns]"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "file_path = '数据预处理2—KNN填充生命体征（连续型）准备数据.xlsx'\n",
    "data = pd.read_excel(file_path)\n",
    "data.head()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "1442da51",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>受试者编号</th>\n",
       "      <th>性别编码</th>\n",
       "      <th>年龄段编码</th>\n",
       "      <th>BMI编码</th>\n",
       "      <th>手术史_分类</th>\n",
       "      <th>既往史_分类</th>\n",
       "      <th>镇静药名称</th>\n",
       "      <th>petco200</th>\n",
       "      <th>RR00</th>\n",
       "      <th>spo200</th>\n",
       "      <th>...</th>\n",
       "      <th>IPI10</th>\n",
       "      <th>moaas10</th>\n",
       "      <th>sbpjieshu</th>\n",
       "      <th>dbpjieshu</th>\n",
       "      <th>petco2jieshu</th>\n",
       "      <th>RRjieshu</th>\n",
       "      <th>spo2jieshu</th>\n",
       "      <th>HRjieshu</th>\n",
       "      <th>IPIjieshu</th>\n",
       "      <th>moaasjieshu</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>38.0</td>\n",
       "      <td>20.0</td>\n",
       "      <td>100.0</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>117.0</td>\n",
       "      <td>78.0</td>\n",
       "      <td>8.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>100.0</td>\n",
       "      <td>88.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>37.0</td>\n",
       "      <td>19.0</td>\n",
       "      <td>99.0</td>\n",
       "      <td>...</td>\n",
       "      <td>4.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>129.0</td>\n",
       "      <td>84.0</td>\n",
       "      <td>19.0</td>\n",
       "      <td>15.0</td>\n",
       "      <td>100.0</td>\n",
       "      <td>82.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>35.0</td>\n",
       "      <td>23.0</td>\n",
       "      <td>100.0</td>\n",
       "      <td>...</td>\n",
       "      <td>8.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>114.0</td>\n",
       "      <td>76.0</td>\n",
       "      <td>20.0</td>\n",
       "      <td>23.0</td>\n",
       "      <td>99.0</td>\n",
       "      <td>72.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>29.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>98.0</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>105.0</td>\n",
       "      <td>55.0</td>\n",
       "      <td>11.0</td>\n",
       "      <td>13.0</td>\n",
       "      <td>98.0</td>\n",
       "      <td>68.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>40.0</td>\n",
       "      <td>10.0</td>\n",
       "      <td>100.0</td>\n",
       "      <td>...</td>\n",
       "      <td>3.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>98.0</td>\n",
       "      <td>65.0</td>\n",
       "      <td>45.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>100.0</td>\n",
       "      <td>83.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 83 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   受试者编号  性别编码  年龄段编码  BMI编码  手术史_分类  既往史_分类  镇静药名称  petco200  RR00  spo200  \\\n",
       "0      1     1      3      1       0       0      0      38.0  20.0   100.0   \n",
       "1      2     1      3      2       0       0      0      37.0  19.0    99.0   \n",
       "2      3     0      2      3       0       0      1      35.0  23.0   100.0   \n",
       "3      4     1      3      2       0       2      1      29.0   6.0    98.0   \n",
       "4      5     1      2      1       0       0      1      40.0  10.0   100.0   \n",
       "\n",
       "   ...  IPI10  moaas10  sbpjieshu  dbpjieshu  petco2jieshu  RRjieshu  \\\n",
       "0  ...    NaN      NaN      117.0       78.0           8.0       0.0   \n",
       "1  ...    4.0      0.0      129.0       84.0          19.0      15.0   \n",
       "2  ...    8.0      1.0      114.0       76.0          20.0      23.0   \n",
       "3  ...    NaN      NaN      105.0       55.0          11.0      13.0   \n",
       "4  ...    3.0      0.0       98.0       65.0          45.0       5.0   \n",
       "\n",
       "   spo2jieshu  HRjieshu  IPIjieshu  moaasjieshu  \n",
       "0       100.0      88.0        1.0          0.0  \n",
       "1       100.0      82.0        4.0          0.0  \n",
       "2        99.0      72.0        5.0          0.0  \n",
       "3        98.0      68.0        4.0          0.0  \n",
       "4       100.0      83.0        6.0          0.0  \n",
       "\n",
       "[5 rows x 83 columns]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 删去没有缺失值的列\n",
    "target_y_columns = [\n",
    "    'sbp00', 'dbp00', 'petco200', 'RR00', 'spo200', 'HR00', 'IPI00', 'moaas00',\n",
    "    'petco2005', 'RR005', 'spo2005', 'HR005', 'IPI005', 'moaas005', 'sbp1', 'dbp1',\n",
    "    'petco21', 'RR1', 'spo21', 'HR1', 'IPI1', 'moaas1', 'sbpjinjing', 'dbpjinjing',\n",
    "    'petco2jinjing', 'RRjinjing', 'spo2jinjing', 'HRjinjing', 'IPIjinjing', 'moaasjinjing',\n",
    "    'petco2015', 'RR015', 'spo2015', 'HR015', 'IPI015', 'moaas015', 'sbp2', 'dbp2',\n",
    "    'petco22', 'RR2', 'spo22', 'HR2', 'IPI2', 'moaas2', 'petco2025', 'RR025', 'spo2025',\n",
    "    'HR025', 'IPI025', 'moaas025', 'sbp3', 'dbp3', 'petco23', 'RR3', 'spo23', 'HR3',\n",
    "    'IPI3', 'moaas3', 'sbp5', 'dbp5', 'petco25', 'RR5', 'spo25', 'HR5', 'IPI5', 'moaas5',\n",
    "    'sbp7', 'dbp7', 'petco27', 'RR7', 'spo27', 'HR7', 'IPI7', 'moaas7', 'petco210', 'RR10',\n",
    "    'spo210', 'HR10', 'IPI10', 'moaas10', 'sbpjieshu', 'dbpjieshu', 'petco2jieshu', 'RRjieshu',\n",
    "    'spo2jieshu', 'HRjieshu', 'IPIjieshu', 'moaasjieshu'\n",
    "]\n",
    "\n",
    "# Find columns in target Y that have no missing values\n",
    "no_missing_columns = [col for col in target_y_columns if data[col].isnull().sum() == 0]\n",
    "\n",
    "# Drop these columns from the dataframe\n",
    "data.drop(columns=no_missing_columns, inplace=True)\n",
    "\n",
    "# Check the first few rows of the dataframe after dropping the columns\n",
    "data.head()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "00a981d4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>性别编码</th>\n",
       "      <th>年龄段编码</th>\n",
       "      <th>BMI编码</th>\n",
       "      <th>手术史_分类</th>\n",
       "      <th>既往史_分类</th>\n",
       "      <th>镇静药名称</th>\n",
       "      <th>petco200</th>\n",
       "      <th>RR00</th>\n",
       "      <th>spo200</th>\n",
       "      <th>HR00</th>\n",
       "      <th>...</th>\n",
       "      <th>IPI10</th>\n",
       "      <th>moaas10</th>\n",
       "      <th>sbpjieshu</th>\n",
       "      <th>dbpjieshu</th>\n",
       "      <th>petco2jieshu</th>\n",
       "      <th>RRjieshu</th>\n",
       "      <th>spo2jieshu</th>\n",
       "      <th>HRjieshu</th>\n",
       "      <th>IPIjieshu</th>\n",
       "      <th>moaasjieshu</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>38.0</td>\n",
       "      <td>20.0</td>\n",
       "      <td>100.0</td>\n",
       "      <td>89.0</td>\n",
       "      <td>...</td>\n",
       "      <td>3.8</td>\n",
       "      <td>0.0</td>\n",
       "      <td>117.0</td>\n",
       "      <td>78.0</td>\n",
       "      <td>8.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>100.0</td>\n",
       "      <td>88.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>37.0</td>\n",
       "      <td>19.0</td>\n",
       "      <td>99.0</td>\n",
       "      <td>78.0</td>\n",
       "      <td>...</td>\n",
       "      <td>4.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>129.0</td>\n",
       "      <td>84.0</td>\n",
       "      <td>19.0</td>\n",
       "      <td>15.0</td>\n",
       "      <td>100.0</td>\n",
       "      <td>82.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>35.0</td>\n",
       "      <td>23.0</td>\n",
       "      <td>100.0</td>\n",
       "      <td>77.0</td>\n",
       "      <td>...</td>\n",
       "      <td>8.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>114.0</td>\n",
       "      <td>76.0</td>\n",
       "      <td>20.0</td>\n",
       "      <td>23.0</td>\n",
       "      <td>99.0</td>\n",
       "      <td>72.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>29.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>98.0</td>\n",
       "      <td>81.0</td>\n",
       "      <td>...</td>\n",
       "      <td>3.4</td>\n",
       "      <td>0.0</td>\n",
       "      <td>105.0</td>\n",
       "      <td>55.0</td>\n",
       "      <td>11.0</td>\n",
       "      <td>13.0</td>\n",
       "      <td>98.0</td>\n",
       "      <td>68.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>40.0</td>\n",
       "      <td>10.0</td>\n",
       "      <td>100.0</td>\n",
       "      <td>106.0</td>\n",
       "      <td>...</td>\n",
       "      <td>3.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>98.0</td>\n",
       "      <td>65.0</td>\n",
       "      <td>45.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>100.0</td>\n",
       "      <td>83.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 82 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   性别编码  年龄段编码  BMI编码  手术史_分类  既往史_分类  镇静药名称  petco200  RR00  spo200   HR00  \\\n",
       "0     1      3      1       0       0      0      38.0  20.0   100.0   89.0   \n",
       "1     1      3      2       0       0      0      37.0  19.0    99.0   78.0   \n",
       "2     0      2      3       0       0      1      35.0  23.0   100.0   77.0   \n",
       "3     1      3      2       0       2      1      29.0   6.0    98.0   81.0   \n",
       "4     1      2      1       0       0      1      40.0  10.0   100.0  106.0   \n",
       "\n",
       "   ...  IPI10  moaas10  sbpjieshu  dbpjieshu  petco2jieshu  RRjieshu  \\\n",
       "0  ...    3.8      0.0      117.0       78.0           8.0       0.0   \n",
       "1  ...    4.0      0.0      129.0       84.0          19.0      15.0   \n",
       "2  ...    8.0      1.0      114.0       76.0          20.0      23.0   \n",
       "3  ...    3.4      0.0      105.0       55.0          11.0      13.0   \n",
       "4  ...    3.0      0.0       98.0       65.0          45.0       5.0   \n",
       "\n",
       "   spo2jieshu  HRjieshu  IPIjieshu  moaasjieshu  \n",
       "0       100.0      88.0        1.0          0.0  \n",
       "1       100.0      82.0        4.0          0.0  \n",
       "2        99.0      72.0        5.0          0.0  \n",
       "3        98.0      68.0        4.0          0.0  \n",
       "4       100.0      83.0        6.0          0.0  \n",
       "\n",
       "[5 rows x 82 columns]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.impute import KNNImputer\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "\n",
    "# Separate the feature set X and target set Y\n",
    "X = data[['性别编码', '年龄段编码', 'BMI编码', '手术史_分类', '既往史_分类', '镇静药名称']]\n",
    "Y = data.drop(columns=['受试者编号', '性别编码', '年龄段编码', 'BMI编码', '手术史_分类', '既往史_分类', '镇静药名称'])\n",
    "\n",
    "scaler_X = StandardScaler()\n",
    "X_scaled = scaler_X.fit_transform(X)\n",
    "\n",
    "scaler_Y = StandardScaler()\n",
    "Y_scaled = scaler_Y.fit_transform(Y)\n",
    "\n",
    "# Initialize the KNN imputer\n",
    "knn_imputer = KNNImputer(n_neighbors=5)\n",
    "\n",
    "# Fit the imputer on the scaled target Y and transform it to impute missing values\n",
    "Y_imputed_scaled = knn_imputer.fit_transform(Y_scaled)\n",
    "\n",
    "# Reverse the standardization to get the imputed target Y in its original scale\n",
    "Y_imputed = scaler_Y.inverse_transform(Y_imputed_scaled)\n",
    "\n",
    "# Create a dataframe for the imputed target Y\n",
    "Y_imputed_df = pd.DataFrame(Y_imputed, columns=Y.columns)\n",
    "\n",
    "# Combine the feature set X with the imputed target Y\n",
    "data_imputed = pd.concat([X, Y_imputed_df], axis=1)\n",
    "\n",
    "# Check the first few rows of the dataframe after imputation\n",
    "data_imputed.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "0994beb0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>受试者编号</th>\n",
       "      <th>性别编码</th>\n",
       "      <th>年龄段编码</th>\n",
       "      <th>BMI编码</th>\n",
       "      <th>手术史_分类</th>\n",
       "      <th>既往史_分类</th>\n",
       "      <th>镇静药名称</th>\n",
       "      <th>petco200</th>\n",
       "      <th>RR00</th>\n",
       "      <th>spo200</th>\n",
       "      <th>...</th>\n",
       "      <th>moaas00</th>\n",
       "      <th>moaas005</th>\n",
       "      <th>moaas1</th>\n",
       "      <th>sbpjinjing</th>\n",
       "      <th>dbpjinjing</th>\n",
       "      <th>moaasjinjing</th>\n",
       "      <th>moaas015</th>\n",
       "      <th>sbp2</th>\n",
       "      <th>dbp2</th>\n",
       "      <th>moaas2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>38.0</td>\n",
       "      <td>20.0</td>\n",
       "      <td>100.0</td>\n",
       "      <td>...</td>\n",
       "      <td>5</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>167</td>\n",
       "      <td>71</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>122</td>\n",
       "      <td>63</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>37.0</td>\n",
       "      <td>19.0</td>\n",
       "      <td>99.0</td>\n",
       "      <td>...</td>\n",
       "      <td>5</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>105</td>\n",
       "      <td>73</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>113</td>\n",
       "      <td>76</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>35.0</td>\n",
       "      <td>23.0</td>\n",
       "      <td>100.0</td>\n",
       "      <td>...</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>129</td>\n",
       "      <td>81</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>129</td>\n",
       "      <td>81</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>29.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>98.0</td>\n",
       "      <td>...</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>149</td>\n",
       "      <td>68</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>117</td>\n",
       "      <td>58</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>40.0</td>\n",
       "      <td>10.0</td>\n",
       "      <td>100.0</td>\n",
       "      <td>...</td>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>110</td>\n",
       "      <td>66</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>110</td>\n",
       "      <td>66</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1237</th>\n",
       "      <td>1242</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>40.0</td>\n",
       "      <td>21.0</td>\n",
       "      <td>100.0</td>\n",
       "      <td>...</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>113</td>\n",
       "      <td>53</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>96</td>\n",
       "      <td>54</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1238</th>\n",
       "      <td>1243</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>26.0</td>\n",
       "      <td>14.0</td>\n",
       "      <td>98.0</td>\n",
       "      <td>...</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>163</td>\n",
       "      <td>59</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>112</td>\n",
       "      <td>47</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1239</th>\n",
       "      <td>1244</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>33.0</td>\n",
       "      <td>15.0</td>\n",
       "      <td>100.0</td>\n",
       "      <td>...</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>110</td>\n",
       "      <td>70</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>102</td>\n",
       "      <td>74</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1240</th>\n",
       "      <td>1245</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>35.0</td>\n",
       "      <td>18.0</td>\n",
       "      <td>100.0</td>\n",
       "      <td>...</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>101</td>\n",
       "      <td>53</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>96</td>\n",
       "      <td>61</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1241</th>\n",
       "      <td>1246</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>41.0</td>\n",
       "      <td>18.0</td>\n",
       "      <td>100.0</td>\n",
       "      <td>...</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>104</td>\n",
       "      <td>67</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>108</td>\n",
       "      <td>68</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1242 rows × 95 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      受试者编号  性别编码  年龄段编码  BMI编码  手术史_分类  既往史_分类  镇静药名称  petco200  RR00  \\\n",
       "0         1     1      3      1       0       0      0      38.0  20.0   \n",
       "1         2     1      3      2       0       0      0      37.0  19.0   \n",
       "2         3     0      2      3       0       0      1      35.0  23.0   \n",
       "3         4     1      3      2       0       2      1      29.0   6.0   \n",
       "4         5     1      2      1       0       0      1      40.0  10.0   \n",
       "...     ...   ...    ...    ...     ...     ...    ...       ...   ...   \n",
       "1237   1242     1      2      1       4       4      0      40.0  21.0   \n",
       "1238   1243     1      3      1       0       0      0      26.0  14.0   \n",
       "1239   1244     1      2      1       4       0      0      33.0  15.0   \n",
       "1240   1245     1      2      2       0       0      0      35.0  18.0   \n",
       "1241   1246     1      3      2       0       0      0      41.0  18.0   \n",
       "\n",
       "      spo200  ...  moaas00  moaas005  moaas1  sbpjinjing  dbpjinjing  \\\n",
       "0      100.0  ...        5         4       2         167          71   \n",
       "1       99.0  ...        5         4       2         105          73   \n",
       "2      100.0  ...        5         3       2         129          81   \n",
       "3       98.0  ...        5         3       2         149          68   \n",
       "4      100.0  ...        5         2       1         110          66   \n",
       "...      ...  ...      ...       ...     ...         ...         ...   \n",
       "1237   100.0  ...        5         3       0         113          53   \n",
       "1238    98.0  ...        5         3       0         163          59   \n",
       "1239   100.0  ...        5         3       0         110          70   \n",
       "1240   100.0  ...        5         3       0         101          53   \n",
       "1241   100.0  ...        5         3       0         104          67   \n",
       "\n",
       "      moaasjinjing  moaas015  sbp2  dbp2  moaas2  \n",
       "0                0         0   122    63       0  \n",
       "1                0         0   113    76       0  \n",
       "2                0         2   129    81       1  \n",
       "3                0         0   117    58       0  \n",
       "4                0         0   110    66       0  \n",
       "...            ...       ...   ...   ...     ...  \n",
       "1237             0         0    96    54       0  \n",
       "1238             0         0   112    47       0  \n",
       "1239             0         0   102    74       0  \n",
       "1240             0         0    96    61       0  \n",
       "1241             0         0   108    68       0  \n",
       "\n",
       "[1242 rows x 95 columns]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 加上没有缺失值的列\n",
    "import pandas as pd\n",
    "\n",
    "file_path = '数据预处理2—KNN填充生命体征（连续型）准备数据.xlsx'\n",
    "data = pd.read_excel(file_path)\n",
    "\n",
    "columns_with_no_missing_values = data.columns[data.isnull().sum() == 0]\n",
    "data_with_no_missing_values = data[columns_with_no_missing_values].iloc[:, -12:]\n",
    "\n",
    "\n",
    "data_imputed = pd.concat([data_imputed, data_with_no_missing_values], axis=1)\n",
    "data_imputed = pd.concat([data['受试者编号'], data_imputed], axis=1)\n",
    "data_imputed.to_excel(r\"数据预处理3——填充生命体征完成数据.xlsx\", index=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2bb96263",
   "metadata": {},
   "source": [
    "##  2、填充术后满意度数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "396f9b01",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>受试者编号</th>\n",
       "      <th>性别编码</th>\n",
       "      <th>年龄段编码</th>\n",
       "      <th>BMI编码</th>\n",
       "      <th>手术史_分类</th>\n",
       "      <th>既往史_分类</th>\n",
       "      <th>镇静药名称</th>\n",
       "      <th>术后24内患者的满意度评价</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>4.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>4.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>3.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>4.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   受试者编号  性别编码  年龄段编码  BMI编码  手术史_分类  既往史_分类  镇静药名称  术后24内患者的满意度评价\n",
       "0      1     1      3      1       0       0      0            5.0\n",
       "1      2     1      3      2       0       0      0            4.0\n",
       "2      3     0      2      3       0       0      1            4.0\n",
       "3      4     1      3      2       0       2      1            3.0\n",
       "4      5     1      2      1       0       0      1            4.0"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "# Load the Excel file\n",
    "file_path = '数据预处理4—KNN填充术后满意度（离散型）准备数据.xlsx'\n",
    "data = pd.read_excel(file_path)\n",
    "\n",
    "# Display the first few rows of the dataframe\n",
    "data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "4add3d54",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "40"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 有多少缺失值？\n",
    "missing_values_count = data['术后24内患者的满意度评价'].isnull().sum()\n",
    "missing_values_count\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "11d5a8c6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>受试者编号</th>\n",
       "      <th>性别编码</th>\n",
       "      <th>年龄段编码</th>\n",
       "      <th>BMI编码</th>\n",
       "      <th>手术史_分类</th>\n",
       "      <th>既往史_分类</th>\n",
       "      <th>镇静药名称</th>\n",
       "      <th>术后24内患者的满意度评价</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>4.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>4.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>3.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>4.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   受试者编号  性别编码  年龄段编码  BMI编码  手术史_分类  既往史_分类  镇静药名称  术后24内患者的满意度评价\n",
       "0      1     1      3      1       0       0      0            5.0\n",
       "1      2     1      3      2       0       0      0            4.0\n",
       "2      3     0      2      3       0       0      1            4.0\n",
       "3      4     1      3      2       0       2      1            3.0\n",
       "4      5     1      2      1       0       0      1            4.0"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 用KNN分类模型进行预测填补\n",
    "from sklearn.impute import KNNImputer\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "from sklearn.neighbors import KNeighborsClassifier\n",
    "import numpy as np\n",
    "\n",
    "# Separate the features (X) and target variable (y)\n",
    "X = data.drop(columns=['术后24内患者的满意度评价'])\n",
    "y = data['术后24内患者的满意度评价']\n",
    "\n",
    "# Split the data into incomplete and complete parts\n",
    "X_incomplete = X[y.isnull()]\n",
    "y_incomplete = y[y.isnull()]\n",
    "X_complete = X[y.notnull()]\n",
    "y_complete = y[y.notnull()]\n",
    "\n",
    "# Standardize the data\n",
    "scaler = StandardScaler()\n",
    "X_scaled = scaler.fit_transform(X_complete)\n",
    "\n",
    "# Create a KNN classifier\n",
    "knn = KNeighborsClassifier(n_neighbors=5)\n",
    "\n",
    "# Fit the classifier to the complete data\n",
    "knn.fit(X_scaled, y_complete)\n",
    "\n",
    "# Predict the missing values using the trained classifier\n",
    "y_pred = knn.predict(scaler.transform(X_incomplete))\n",
    "\n",
    "# Replace the missing values in the original data with the predicted values\n",
    "data.loc[data['术后24内患者的满意度评价'].isnull(), '术后24内患者的满意度评价'] = y_pred\n",
    "\n",
    "# Display the first few rows of the dataframe after imputation\n",
    "data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "bb4107dd",
   "metadata": {},
   "outputs": [],
   "source": [
    "output_file_path = '数据预处理5—填充术后满意度完成数据.xlsx'\n",
    "data.to_excel(output_file_path, index=False)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
